home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 August: Tool Chest / Apple_Developer_Group_August_1996_Tool_Chest.iso / Sample Code / Snippets / Toolbox / Icon play / PlotIcons.c next >
Encoding:
C/C++ Source or Header  |  1994-10-28  |  9.5 KB  |  364 lines  |  [TEXT/KAHL]

  1. // snippet to demonstrate the use of the system 7 icon utilities
  2. // and the system 7 popup control cdef in a program using modal dialogs
  3. //
  4. // This shows: how to use the sys 7.0 popup
  5. // and how to plot Icons from Icon families.
  6. //
  7. // Nick Thompson, 6 May '94
  8. //
  9. //    5/11/94        nt        updated to use std file icons, in another dialog box
  10. //
  11. //    Copyright:    © 1994 by Apple Computer, Inc., all rights reserved.
  12.  
  13.  
  14. #include <icons.h>
  15.  
  16. // prototypes
  17. pascal Boolean OurFilter(DialogPtr dlg, EventRecord *event, short *itemHit) ;
  18.  
  19. // constants used in the dialog filter proc
  20. const char kEnter    = 0x03 ;
  21. const char kReturn    = 0x0D ;
  22. const char kEscape    = 0x1B ;
  23. const char kPeriod    = '.' ;
  24.  
  25.  
  26. // constants for our dialog IDs these are used in the filterproc to do the "right thing"
  27. // by calling the appropriate redrawing code
  28.  
  29. const long    kIconsDialog        = 1000 ;
  30. const long    kStdFileIconsDialog    = 1001 ;
  31.  
  32.  
  33. // constants for items in the dialog
  34. enum {
  35.     kThePopupMenu = 4 ,        // popup menu is the fourth item in the dialog
  36.     kUserTextArea = 5 ,        // this is a rect for us to write a string with the user selection
  37.     kLargeIconArea = 8 ,    // rect to draw the lg icon into
  38.     kSmallIconArea = 9 ,    // rect to draw the small icon
  39.     kMoreIconBtn = 10         // we want to see the std file icon dialog
  40. } ;
  41.  
  42. // table to tie up the menu selection with the 
  43. // actual icon IDs defined in Icons.h
  44. // These are documented in the resource manager chapter
  45. // of Inside Macintosh: More Toolbox Essentials, Page 1-130 onwards.
  46.  
  47. long    resIDTable[] = {
  48.         0,                                    /* Dummy for start of the array */
  49.         genericDocumentIconResource,        /* [1]  "Generic Document" */
  50.         genericApplicationIconResource,        /* [2]  "Generic Application" */
  51.         genericDeskAccessoryIconResource,    /* [3]  "Generic Desk Accessory" */
  52.         genericEditionFileIconResource,        /* [4]  "Generic Edition File" */
  53.         genericStationeryIconResource,        /* [5]  "Generic Stationary" */
  54.         genericPreferencesIconResource,        /* [6]  "Generic Preferences" */
  55.         genericQueryDocumentIconResource,    /* [7]  "Generic Query Document" */
  56.         genericExtensionIconResource,        /* [8]  "Generic Extension" */
  57.         genericFolderIconResource,            /* [9]  "Generic Folder" */
  58.         privateFolderIconResource,            /* [10]  "Private Folder" */
  59.         ownedFolderIconResource,            /* [11]  "Owned Folder" */
  60.         dropFolderIconResource,                /* [12]  "Drop Folder" */
  61.         sharedFolderIconResource,            /* [13]  "Shared Folder" */
  62.         mountedFolderIconResource,            /* [14]  "Mounted Folder" */
  63.         systemFolderIconResource,            /* [15]  "System Folder" */
  64.         appleMenuFolderIconResource,        /* [16]  "Apple Menu Folder" */
  65.         startupFolderIconResource,            /* [17]  "Startup Items Folder" */
  66.         controlPanelFolderIconResource,        /* [18]  "Control Panels" */
  67.         printMonitorFolderIconResource,        /* [19]  "Printmonitor Folder" */
  68.         preferencesFolderIconResource,        /* [20]  "Preferences Folder" */
  69.         extensionsFolderIconResource,        /* [21]  "Extensions" */
  70.         -3968,                                /* [22]  "Fonts" */
  71.         floppyIconResource,                    /* [23]  "Floppy Icon" */
  72.         trashIconResource,                    /* [24]  "Trash Icon" */
  73.         fullTrashIconResource                /* [25]  "Full Trash Icon" */
  74. } ;
  75.  
  76. long    stdFileResTablep[] = {
  77.         1,
  78.         2,
  79.         3,
  80.         4,
  81.         5
  82. } ;
  83.  
  84.  
  85.  
  86. // Structure for the private data for a popup control.
  87. // This structure is documented on page 5-77 
  88. // Inside Macintosh: Macintosh Toolbox Essentials
  89.  
  90. typedef struct popupPrivateData {
  91.     MenuHandle     mHandle;     // the popup menu handle 
  92.     short         mID;        // the popup menu ID 
  93.     // after these two public fields is the mPrivate private data, 
  94.     // which may be any old size and should not be messed with 
  95. }    popupPrivateData;
  96.  
  97.  
  98. void DoIconsDialog(void) ;
  99.  
  100.  
  101. main() {
  102.  
  103.     // initialize the toolbox
  104.     InitGraf(&qd.thePort); InitFonts(); InitWindows(); InitMenus();
  105.     TEInit(); InitDialogs(nil); InitCursor(); FlushEvents(everyEvent,0);
  106.     
  107.     
  108.     DoIconsDialog() ;
  109. }
  110.  
  111.  
  112. void DoStdFileIconsDialog(void) 
  113. {
  114.     popupPrivateData    **myPopupPrivateDataPtr ;
  115.     
  116.     short                iKind;
  117.     Handle                iHandle;
  118.     Rect                iRect;
  119.     
  120.     MenuHandle            thePopupMenuHdl ;
  121.     Str255                theItem ;
  122.     
  123.     OSErr                theErr ;
  124.     
  125.     short                userSelection ;            // the current menu selection
  126.     DialogPtr             thePopupDialog ;
  127.     short                itemHit ;
  128.     GrafPtr                savedPort ;
  129.     
  130.     short                index ;
  131.  
  132.     thePopupDialog = GetNewDialog ( 129, nil, (WindowPtr)-1 );
  133.     SetWRefCon((WindowPtr)thePopupDialog, kStdFileIconsDialog ) ;
  134.     GetPort( &savedPort ) ;
  135.     SetPort( thePopupDialog ) ;
  136.  
  137.  
  138.     // get a reference to the popup control, this is item 4 in the dialog
  139.     // we need to get the menuhandle associated with the control, it is in the private
  140.     // control data field, as documented in Inside Macintosh: Toolbox page 5-77
  141.     
  142.     // get the control handle for the popup            
  143.     GetDItem ( thePopupDialog, kThePopupMenu, &iKind, &iHandle, &iRect) ;
  144.     
  145.     // extract from the control the menuhandle
  146.     myPopupPrivateDataPtr = (popupPrivateData **)(**(ControlHandle)iHandle).contrlData ; 
  147.     thePopupMenuHdl = (**myPopupPrivateDataPtr).mHandle ;
  148.     
  149.     // set up the Icons - we have 5 items here
  150.     for( index = 0;    index < 5; index++ ) 
  151.         SetItemIcon( thePopupMenuHdl, index + 1, stdFileResTablep[index]) ;
  152.     do {
  153.     
  154.         ModalDialog ( OurFilter, &itemHit );
  155.         
  156.         switch ( itemHit ) {
  157.         
  158.             default :
  159.                 break ;        
  160.         }
  161.         
  162.     } while( itemHit != ok ) ;
  163.     
  164.     DisposDialog ( thePopupDialog );
  165.     SetPort( savedPort ) ;
  166.  
  167. }
  168.  
  169.  
  170.  
  171. // this function ensures that the correct Icons are drawn in the dialog
  172. // corresponding with the current user selection.
  173.  
  174. UpdateIconsDialog(    DialogPtr thePopupDialog )     // dialog pointer for the popup dialog
  175. {
  176.     popupPrivateData    **myPopupPrivateDataPtr ;
  177.     
  178.     short                iKind;
  179.     Handle                iHandle;
  180.     Rect                iRect;
  181.     
  182.     MenuHandle            thePopupMenuHdl ;
  183.     Str255                theItem ;
  184.     
  185.     OSErr                theErr ;
  186.     
  187.     short                userSelection ;            // the current menu selection
  188.     
  189.     // the user choose the popup.  The item number selected will be the control value
  190.     // we need to get the menuhandle associated with the control, it is in the private
  191.     // control data field, as documented in Inside Macintosh: Toolbox page 5-77
  192.     
  193.     // get the control handle for the popup            
  194.     GetDItem ( thePopupDialog, kThePopupMenu, &iKind, &iHandle, &iRect) ;
  195.     
  196.     // extract from the control the menuhandle
  197.     myPopupPrivateDataPtr = (popupPrivateData **)(**(ControlHandle)iHandle).contrlData ; 
  198.     thePopupMenuHdl = (**myPopupPrivateDataPtr).mHandle ;
  199.  
  200.     // get the string associated with the users selection
  201.     userSelection = GetCtlValue((ControlHandle)iHandle) ;
  202.     GetItem ( thePopupMenuHdl, userSelection, theItem );
  203.     
  204.     // get the rect we are drawing in
  205.     GetDItem ( thePopupDialog, kUserTextArea, &iKind, &iHandle, &iRect) ;
  206.     
  207.     // erase the area to draw in
  208.     EraseRect( &iRect ) ;
  209.                 
  210.     // and draw the current user selection
  211.     MoveTo(iRect.left + 1, iRect.bottom - 6) ;
  212.     DrawString( theItem ) ;
  213.     
  214.     // now plot the Icons.  The id of the item selected in
  215.     // the menu will be in userSelection.
  216.     
  217.     // first draw the large icon
  218.     
  219.     // get the rect we are drawing in
  220.     GetDItem ( thePopupDialog, kLargeIconArea, &iKind, &iHandle, &iRect) ;
  221.  
  222.     // erase the area to draw in
  223.     EraseRect( &iRect ) ;
  224.  
  225.     theErr = PlotIconID( &iRect, atVerticalCenter, ttNone, resIDTable[ userSelection ] ) ;
  226.     
  227.     // now draw the small icon
  228.     
  229.     // get the rect we are drawing in
  230.     GetDItem ( thePopupDialog, kSmallIconArea, &iKind, &iHandle, &iRect) ;
  231.  
  232.     // erase the area to draw in
  233.     EraseRect( &iRect ) ;
  234.  
  235.     theErr = PlotIconID( &iRect, atVerticalCenter, ttNone, resIDTable[ userSelection ] ) ;
  236.  
  237.  
  238.  
  239. }
  240.  
  241.  
  242. void DoIconsDialog(void) 
  243. {
  244.     DialogPtr             thePopupDialog ;
  245.     short                itemHit ;
  246.     GrafPtr                savedPort ;
  247.  
  248.     thePopupDialog = GetNewDialog ( 128, nil, (WindowPtr)-1 );
  249.     
  250.     SetWRefCon((WindowPtr)thePopupDialog, kIconsDialog ) ;
  251.     GetPort( &savedPort ) ;
  252.     SetPort( thePopupDialog ) ;
  253.     UpdateIconsDialog( thePopupDialog ) ;
  254.  
  255.  
  256.     // get a reference to the popup control, this is item 4 in the dialog
  257.     
  258.     do {
  259.     
  260.         ModalDialog ( OurFilter, &itemHit );
  261.         
  262.         switch ( itemHit ) {
  263.         
  264.             case kThePopupMenu :
  265.             
  266.                 UpdateIconsDialog( thePopupDialog ) ;
  267.                 // reset itemHit to something else or we'll continually redraw
  268.                 itemHit = 0 ;
  269.                 break ;
  270.                 
  271.             case kMoreIconBtn :
  272.                 
  273.                 SetPort( savedPort ) ;
  274.                 DoStdFileIconsDialog() ;
  275.                 SetPort( thePopupDialog ) ;
  276.                 break ;
  277.         
  278.         }
  279.         
  280.     } while( itemHit != ok ) ;
  281.     
  282.     DisposDialog ( thePopupDialog );
  283.     SetPort( savedPort ) ;
  284.  
  285. }
  286.  
  287.  
  288. void    FlashDialogItem(DialogPtr dlg, short itemToFlash)
  289. {
  290.     short    iKind;
  291.     Handle    iHandle;
  292.     Rect    iRect;
  293.     long    ignored;
  294.  
  295.     GetDItem(dlg, itemToFlash, &iKind, &iHandle, &iRect);
  296.     HiliteControl((ControlHandle) iHandle, 1);
  297.     Delay(8, &ignored);
  298.     HiliteControl((ControlHandle) iHandle, 0);
  299. }
  300.  
  301.  
  302.  
  303.  
  304. pascal Boolean OurFilter(DialogPtr dlg, EventRecord *event, short *itemHit)
  305. {
  306.     short    iKind;
  307.     Handle    iHandle;
  308.     Rect    iRect;
  309.     char    key;
  310.     short    radius;
  311.  
  312.     switch (event->what) {
  313.  
  314.         case nullEvent:
  315.             break;
  316.  
  317.         case keyDown:
  318.         case autoKey:
  319.             key = event->message & charCodeMask;
  320.             if (event->modifiers & cmdKey) {        /* Command key down */
  321.                 if (key == kPeriod) {
  322.                     *itemHit = ok;
  323.                     FlashDialogItem(dlg, *itemHit);
  324.                 }
  325.                 return TRUE;            /* This says we handle ALL command keys */
  326.             } else {
  327.                 if ((key == kReturn) || (key == kEnter)) {
  328.                     *itemHit = ok;
  329.                     FlashDialogItem(dlg, *itemHit);
  330.                     return TRUE;
  331.                 }
  332.                 if (key == kEscape) {
  333.                     *itemHit = ok;
  334.                     FlashDialogItem(dlg, *itemHit);
  335.                     return TRUE;
  336.                 }
  337.             }
  338.             return FALSE;
  339.  
  340.         case updateEvt:
  341.             SetPort(dlg);
  342.                 
  343.             GetDItem(dlg, ok, &iKind, &iHandle, &iRect);            
  344.             InsetRect(&iRect, -4, -4);
  345.             radius = (iRect.bottom - iRect.top) / 2;
  346.             if (radius < 16)
  347.                 radius = 16;
  348.                 
  349.             PenSize(3,3);
  350.             FrameRoundRect(&iRect, radius, radius);
  351.             PenNormal();
  352.             
  353.             if(    GetWRefCon((WindowPtr)dlg ) == kIconsDialog ) 
  354.                 UpdateIconsDialog( dlg ) ;
  355.  
  356.  
  357.             return FALSE;
  358.  
  359.         default:
  360.             return FALSE;
  361.     }
  362. }
  363.  
  364.